<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule;

use OverFish, Nette, AdminModule\Forms\GalleryForm, AdminModule\Forms\GalleryUploadForm;

class GalleryPresenter extends BaseAdminPresenter {
    
    public $title = "Obsah";
    private $id = False;
    protected $menuItem = "gallery";
    
    public function renderDefault() {
        $this->heading = "Fotogalerie";
    }
    
    public function renderNew() {
        $this->heading = "Nová galerie"; 
    }
    
    public function renderEdit($id) {
        $this->heading = "Upravit galerii";
        $this->id = $id;
    }
    
    public function renderDelete($id) {

        $item = $this->repositories->gallery->find($id);
        $this->repositories->gallery->delete($item);
        
        $this->flashMessage('Galerie byla úspěšně smazána.', 'success');
                
        $this->redirect(':Admin:Gallery:default');
    }
    
    public function renderActive($id) {
        $item = $this->repositories->gallery->find($id);
        
        if ($item->getActive()) {
            $item->setActive(False);
        } else {
            $item->setActive(True);
        }
        
        
        $this->repositories->gallery->save($item);
        
        if ($item->getActive()) {
            $this->flashMessage('Galerie byla úspěšně nastavena jako aktivní.', 'success');
        } else {
            $this->flashMessage('Galerie byla úspěšně nastavena jako neaktivní.', 'success');
        }
           
        $this->redirect(':Admin:Gallery:default');
        
    }
    
    public function renderPhoto($id) {
        $item = $this->repositories->gallery->find($id);
        $this->template->galleryName = $item->getTitle();
        $this->id = $id;
    }
    
    protected function createComponentGalleryForm($name)
    {
        $form = new GalleryForm($this, $name);
        
        if ($this->id) {
            $item = $this->repositories->gallery->find($this->id);
            
            $form->setDefaults(array(
                "id" => $item->getId(),
                "title" => $item->getTitle(),
                "content" => $item->getContent(),
                "active" => $item->getActive(),
                "id_menu" => $item->getMenuItemId(),
            ));
        } else {
            $form->setDefaults(array(
                "active" => True,
            ));
        }
    }
    
    protected function createComponentGalleryUploadForm($name)
    {
        $form = new GalleryUploadForm($this, $name);
        
        if ($this->id) {
            $item = $this->repositories->gallery->find($this->id);
            
            $form->setDefaults(array(
                "id" => $item->getId()
            ));
        } else {
            $form->setDefaults(array());
        }
        
        return $form;
    }
    
    protected function createComponentGrid() {
        $datasource = $this->repositories->gallery->getDataSource();

        $grid = new OverFish\Controls\Grid();
        $grid->setDataSource($datasource);

        $grid->addColumn("title", "Titulek", array("sortable" => True));
        $grid->addColumn("content", "Text");
        $grid->addColumn("active", "Aktivní");
        $grid->addColumn("x_created", "Datum vytvoření", array("sortable" => True));

        // action buttons
        $presenter = $this;
            
        $grid->addButton("edit", "Upravit", array(
            "handler" => function ($gallery) use ($presenter) {
                $presenter->redirect(":Admin:Gallery:edit", array("id" => $gallery->id));
            },
            "icon" => "ui-icon-pencil",
            "showText" => false,
        ));
            
            
        $grid->addButton("delete", "Smazat", array(
            "handler" => function ($gallery) use ($presenter) {
                $presenter->redirect(":Admin:Gallery:delete", array("id" => $gallery->id));
            },
            "icon" => "ui-icon-trash",
            "showText" => false,
        ));
        
            
        $grid->addButton("active", "Aktivní/Neaktivní", array(
            "handler" => function ($gallery) use ($presenter) {
                $presenter->redirect(":Admin:Gallery:active", array("id" => $gallery->id));
            },
            "icon" => "ui-icon-power",
            "showText" => false,
        ));
            
        $grid->addButton("photo", "Forografie", array(
            "handler" => function ($gallery) use ($presenter) {
                $presenter->redirect(":Admin:Gallery:photo", array("id" => $gallery->id));
            },
            "icon" => "ui-icon-image",
            "showText" => false,
        ));
            
        return $grid;
    }

}

